package com.shijie.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import com.shijie.component.ErpUserService;
import com.shijie.core.AbstractService;
import com.shijie.core.ServiceException;
import com.shijie.domain.entity.Admins;
import com.shijie.domain.entity.Brands;
import com.shijie.domain.entity.Coupons;
import com.shijie.domain.entity.UserCoupons;
import com.shijie.domain.enums.EnumInterface;
import com.shijie.mapper.CouponsMapper;
import com.shijie.mapper.UserCouponsMapper;
import com.shijie.utils.DateUtil;

/*
 *  @author: smart boy
 *  @date: 2018-11-21
 */
@Service
public class CouponsService extends AbstractService<Coupons> {

	@Resource
	private CouponsMapper couponsMapper;
	@Resource
	private ErpUserService erpUserService;
	@Resource
	private BrandsService brandsService;
	@Resource
	private UserCouponsMapper userCouponsMapper;
	
	public void save(Coupons coupons){
		Admins  admin = erpUserService.getAdmin();
		if(admin.getBrandId() == null){
			coupons.setBrandId(0);
			coupons.setBrandName("平台");
		}else{
			coupons.setBrandId(admin.getBrandId());
			Brands brand = brandsService.selectById(admin.getBrandId());
			coupons.setBrandName(brand!=null?brand.getName():null);
		}
		coupons.setReceive(0);
		coupons.setCreateBy(erpUserService.getUserName());
		coupons.setCreateTime(new Date());
		couponsMapper.insertSelective(coupons);
	}
	
	public void updateById(Coupons coupons){
		Condition condition = new Condition(UserCoupons.class);
		condition.createCriteria().andEqualTo("couponId", coupons.getId());
		int count = userCouponsMapper.selectCountByCondition(condition);
		if(count > 0){
			throw new ServiceException("该优惠券已被用户领取，不能修改！");
		}
		couponsMapper.updateByPrimaryKeySelective(coupons);
	}

	/**
	 * 获取用户对应状态下的优惠券
	 *
	 * @param userId
	 * @param brandId
	 * @param status  0 待使用， 1已使用 ， 2 已过期
	 * @return
	 */
	public List<UserCoupons> selectByBrandId(Integer userId, Integer brandId, Integer status) {
		return userCouponsMapper.selectUserCoupons(userId, brandId, status);
	}

	public String InsertUserCoupons(UserCoupons userCoupons) {
		if (userCoupons.getCouponId() == null) {
			return "领取的优惠券必须输入！";
		}
		Coupons coupon = couponsMapper.selectByPrimaryKey(userCoupons.getCouponId());
		if (coupon.getStartDate().compareTo(new Date()) > 0 || coupon.getEndDate().compareTo(new Date()) < 1) {
			return "优惠券活动已经结束，请核实！";
		}
		if (coupon.getCouponCount() < coupon.getReceive() + 1) {
			return "优惠券已经全部领取完毕！";
		}

		Condition condition = new Condition(UserCoupons.class);
		Example.Criteria criteria = condition.createCriteria();
		criteria.andEqualTo("userId", userCoupons.getUserId());
		criteria.andEqualTo("couponId", userCoupons.getCouponId());
		List<UserCoupons> list = userCouponsMapper.selectByCondition(condition);
		if (list != null && list.size() >= coupon.getUserMax()) {
			return "优惠券每人最多领取" + coupon.getUserMax() + "张，已经领取完毕！";
		}
		// 插入数据
		userCoupons.setStatus(EnumInterface.IsUsed.NOUSED.ordinal());
		userCoupons.setStartDate(new Date());
		if (coupon.getValidType() == 0) {
			userCoupons.setEndDate(coupon.getEndDate());
		} else {
			userCoupons.setEndDate(DateUtil.addDays(new Date(), coupon.getValidTime()));
		}
		userCoupons.setCreateDate(new Date());
		userCouponsMapper.insert(userCoupons);
		return "";
	}
}
                                                                                                                                                                                                                           
